

package GUI;

/**
 *
 * @author David Ponce
 * @author Philipp Armbruster
 */

import Interaction.ServerConnection;
import Flightadministration.Flightadministration;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.rmi.RemoteException;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.Vector;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;



public class AircraftTypEdit extends JPanel{

    final Main td;
    public JTextField textfield1;
    public Vector<String> allSeatInformation;
    public JLabel label2;
    public JLabel label5;
    private JPanel jp;
    /*Das AdministrationsObjekt, welches über die getConnection methode vom RMI Server geholt wird, dem "admin" objekt zugewiesen.
    *Dadurch ist eine Kommunikation zwischen GUI und Applikationsserver mittels RMI möglich*/
    final Flightadministration admin = ServerConnection.getConnection();


    // Konstruktor
    public AircraftTypEdit(final Main td) throws RemoteException {
        
        this.td = td;
        showAircraftTypEdit();
    }
    
    // Methoden
    public void showAircraftTypEdit() throws RemoteException{

        jp = new JPanel();

        //GridBagLayout
        //**********************************************************************
        GridBagLayout gbl = new GridBagLayout();
        jp.setLayout(gbl);
        jp.setBackground(Color.white);

        GridBagConstraints constraints = new GridBagConstraints();

        constraints.fill = GridBagConstraints.HORIZONTAL;   // wie Komponente Bereich füllen soll
        constraints.weightx = 1;                     // Breite
        constraints.weighty = 3;                    // Höhe
        constraints.insets = new Insets(0,0,1,1);   // Abstände definieren


        //Label
        //**********************************************************************

        JLabel headline = new JLabel("<html><h2 color=blue>Flugzeugtyp bearbeiten</h2></html>");
        headline.setHorizontalAlignment(JLabel.CENTER);
        constraints.gridx = 0;
        constraints.gridy = 0;
        constraints.gridwidth = 2;
        jp.add(headline, constraints);

        label2 = new JLabel();

        JLabel label3 = new JLabel("Typenbezeichnung: ");
        constraints.gridwidth = 1;
        constraints.gridx = 0;
        constraints.gridy = 1;
        jp.add(label3, constraints);

        textfield1 = new JTextField();
        constraints.gridwidth = 1;
        constraints.gridx = 1;
        constraints.gridy = 1;
        jp.add(textfield1, constraints);

        JLabel label4 = new JLabel("Sitzplatzstruktur: ");
        constraints.gridwidth = 1;
        constraints.gridx = 0;
        constraints.gridy = 2;
        jp.add(label4, constraints);

        label5 = new JLabel();
        constraints.gridwidth = 1;
        constraints.gridx = 1;
        constraints.gridy = 2;
        jp.add(label5, constraints);


        //Buttons
        //**********************************************************************
        
        JButton button_UpdateAircraftType = new JButton("Änderungen speichern");
        constraints.gridwidth = 1;
        constraints.gridx = 0;
        constraints.gridy =3;
        jp.add(button_UpdateAircraftType, constraints);

        JButton button_Cancel = new JButton("Zurück zur Übersicht");
        constraints.gridwidth = 1;
        constraints.gridx = 1;
        constraints.gridy = 3;
        jp.add(button_Cancel, constraints);


        //Action Listener
        //**********************************************************************
        button_UpdateAircraftType.addActionListener(new ActionListener() {
                @Override
            public void actionPerformed(ActionEvent e){
                if ((textfield1.getText().equals("")))
                {
                    JOptionPane.showMessageDialog(null, "Bitte Typenbezeichnung angeben!");
                }
                else {
                            try {
                                String id = label2.getText();
                                int idAsInt = Integer.parseInt(id);

                                // Die Id der ausgewählten Sitzstruktur (Seat) aus Label auslesen
                                String currentSeatAsString = label5.getText();
                                        int index2forSubstring = currentSeatAsString.indexOf("(") - 1;
                                            int currentSeatId = Integer.parseInt(currentSeatAsString.substring(13, index2forSubstring));

                                admin.updateTypeOfAircraft(idAsInt, textfield1.getText(), currentSeatId);
                                    // JTables aktualisieren
                                    td.updateAircraftType();

                                ((CardLayout) td.rightp.getLayout()).show(td.rightp,"AircraftTypeM");

                            } catch (RemoteException ex) {
                                Logger.getLogger(AircraftCreate.class.getName()).log(Level.SEVERE, null, ex);
                            }

                    JOptionPane.showMessageDialog(null, "Flugzeugtyp geändert");

                }
            }
        });

        button_Cancel.addActionListener(new ActionListener() {
                @Override
        public void actionPerformed(ActionEvent e){

            ((CardLayout) td.rightp.getLayout()).show(td.rightp,"AircraftTypeM");

            }
        });
    }

    
    public JPanel getJp() {
        return jp;
    }
}




